﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BE;
using BL;
using System.Data;

using Mapeo;
using System.Collections;
using AjaxControlToolkit;
using NHibernate;

using MySql.Data;
using MySql.Data.MySqlClient;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using System.Diagnostics;
using System.Drawing;
using System.Net;
using System.IO;
//using CrystalDecisions.Windows;

namespace UI.Tesoreria
{
    public partial class frmReporteSalidaCajaLista : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LabelErrorListado.Text = null;
                try
                {
                    LLenarAlumno();                   
                }
                catch (Exception ex)
                {         
             LabelErrorListado.Text = ex.Message;
                }
            }
            else
            {

            }
        }

        private void LLenarAlumno()
        {
            LabelErrorListado.Text = null;
            try
            {
                int codigo = (int)Session["id"];
                string nombre = (String)Session["nombre"];

                lblPersona.Text = nombre;
                //CargarListado();

                SalidaCaja_BL salidabl = new SalidaCaja_BL();

                GridSalidaCajaItem.DataSource = salidabl.Listado_data_Salidacaja_xid(codigo.ToString());
                GridSalidaCajaItem.DataBind();

                decimal sumaTotal1 = 0;
                foreach (GridViewRow row in GridSalidaCajaItem.Rows)
                {
                    sumaTotal1 += Convert.ToDecimal(row.Cells[4].Text);
                }
                lblTotal.Text = sumaTotal1.ToString();
            }
            catch (Exception ex)
            {
                LabelErrorListado.Text = ex.Message;
            }
        }

        private void CargarListado()
        {
            
        }

        protected void btnReporte_Click(object sender, EventArgs e)
        {
            if (GenerarConsultaReporteSalida())
            {
                string script = "AbrirVentana('frmReporteSalidaCajaListaPdf.aspx')";
                ScriptManager.RegisterStartupScript(this, typeof(Page), "popup", script, true);
            };
        }

        private bool GenerarConsultaReporteSalida()
        {
            LabelErrorListado.Text = "";
            try
            {
                ReporteSalidaCaja reporte = new ReporteSalidaCaja();
                ReporteComprobanteVenta_BL rcv = new ReporteComprobanteVenta_BL();


                int codigo = (int)Session["id"];
                SalidaCaja_BL salidabl = new SalidaCaja_BL();

                DataTable data = salidabl.Listado_data_Salidacaja_xid(codigo.ToString()); 

                if (data.Rows.Count == 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('" + "No se encontraron registros :" + "" + "');", true);
                    return false;
                }

                reporte.SetDataSource(data);

                //reporte.DataDefinition.FormulaFields["Docente"].Text = "'" + nombreProfeReporte + "'";

                // reporte.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, true, "CpVenta");

                ExportOptions CrExportOptions;
                DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                //String rutaarchivo = System.IO.Path.GetTempFileName() + "CpVenta-" + Guid.NewGuid() + ".pdf";
                String rutaarchivo = System.IO.Path.GetTempFileName() + Guid.NewGuid() + ".pdf";
                CrDiskFileDestinationOptions.DiskFileName = rutaarchivo;

                Session["RutaReporte"] = rutaarchivo;
                Session["Reporte"] = reporte;


                CrExportOptions = reporte.ExportOptions;
                {
                    CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                    CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                    CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;

                    CrExportOptions.FormatOptions = CrFormatTypeOptions;
                }

                reporte.Export();

                return true;
            }
            catch (Exception ex)
            {
                LabelErrorListado.Text = ex.Message;
                return false;
            }
        }
    }
}